/*
The number, 1406357289, is a 0 to 9 pandigital number because it is made up of each of the digits 0 to 9 in some order, but it also has a rather interesting sub-string divisibility property.
Let d1 be the 1st digit, d2 be the 2nd digit, and so on. In this way, we note the following:
d2d3d4=406 is divisible by 2
d3d4d5=063 is divisible by 3
d4d5d6=635 is divisible by 5
d5d6d7=357 is divisible by 7
d6d7d8=572 is divisible by 11
d7d8d9=728 is divisible by 13
d8d9d10=289 is divisible by 17
Find the sum of all 0 to 9 pandigital numbers with this property.

Anser:16695334890
Time:5.513201ms
*/
package main

import (
	"fmt"
	"time"
)

var count int
var sp []int = []int{2, 3, 5, 7, 11, 13, 17}

func main() {
	tstart := time.Now()
	for i := 1; i < 10; i++ {
		pandigital(i)
	}
	fmt.Println(count)
	tend := time.Now()
	fmt.Println(tend.Sub(tstart))
}

// divisibility 是否全部可被整除
// func divisibility(num int) bool {
// 	s := []int{2, 3, 5, 7, 11, 13, 17}
// 	for i := 2; i < 9; i++ {
// 		newNum := num / int(math.Pow10(8-i)) % 1000
// 		if newNum%s[i-2] > 0 {
// 			return false
// 		}
// 	}
// 	return true
// }

func numLenAndDig(num int) (int, []bool) {
	i := 0
	s := make([]bool, 10)
	for num > 0 {
		s[num%10] = true
		num /= 10
		i++
	}
	return i, s
}
func pandigital(num int) {
	n, s := numLenAndDig(num)

	if n > 3 {
		if num%1000%sp[n-4] > 0 {
			return
		} else if n == 10 {
			count += num
			return
		}
	}
	for k, v := range s {
		if !v {
			pandigital(num*10 + k)
		}
	}
	return
}
